package com.aw.baselib.widgets.font

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import android.widget.TextView
import com.aw.baselib.base.AwBaseApplication

/**
 * @author : hzw
 * https://blog.csdn.net/yuanxw44/article/details/80019501
 */
@SuppressLint("AppCompatCustomView")
class AwTvRegularRed(context: Context?, attrs: AttributeSet?) : TextView(context, attrs) {
    init {
        typeface = AwBaseApplication.instance?.typefaceRegular
    }

    override fun setText(text: CharSequence, type: BufferType) {
        if (text != null && text.length >= 1) {
            val ss = SpannableString(text)
            ss.setSpan(
                ForegroundColorSpan(Color.RED), text.length - 1, text.length,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )
            super.setText(ss, type)
        } else {
            super.setText(text, type)
        }
    }
}